import os
import time


def stop_appium(post_num=4723):
    '''关闭appium服务'''
    p = os.popen('netstat -aon | findstr '+str(post_num))
    p0 = p.read().strip()
    if p0 != '' and 'LISTENING' in p0:
        p1 = int(p0.split('LISTENING')[1].strip()[0:5])
        os.popen('taskkill /F /PID '+str(p1))
        print('appium server 已结束')
    else:
        print('未找到进程号')

def start_appium(post_num=4723):
    '''开启appium服务'''
    stop_appium(post_num)
    cmd = 'start /b appium -a 127.0.0.1 -p '+str(post_num)+' --log appium.log --local-timezone'
    os.system(cmd)
    time.sleep(3)
    print('appium启动成功')